<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data="users" highlight-current-row style="width: 100%;">
			<el-table-column type="index">
			</el-table-column>
			<el-table-column prop="id" label="ID" sortable>
			</el-table-column>
			<el-table-column prop="username" label="用户邮箱" sortable>
			</el-table-column>
			<el-table-column prop="lastLoginDate" :formatter="dateFormat" label="最后登录时间" sortable>
			</el-table-column>
			<el-table-column prop="createdAt" :formatter="dateFormat" label="创建时间" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :before-close="closeAdd">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="用户邮箱" prop="username">
					<el-input v-model="addForm.username"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input type="password" v-model="addForm.password"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="closeAdd">取消</el-button>
				<el-button type="primary" @click.native="addSubmit">提交</el-button>
			</div>
		</el-dialog>
		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :before-close="closeEdit">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="用户邮箱" prop="type">
					<el-input v-model="editForm.username" readonly></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input type="password" v-model="editForm.password"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="closeEdit">取消</el-button>
				<el-button type="primary" @click.native="editSubmit">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>
<script>
import { userService } from 'src/api'
import { dateFormat } from 'src/lib/format';
export default {
	data() {
		return {
			users: [],
			//分页
			total: 0,
			page: 1,
			limit: 20,
			//新增界面
			addFormVisible: false,
			addForm: {
				username: '',
				password: ''
			},
			addFormRules: {
				username: [
					{ required: true, type: 'email', message: '请输入用户邮箱', trigger: 'blur' },
				],
				password: [
					{ required: true, message: '请输入密码', trigger: 'blur' },
				]
			},
			//编辑界面是否显示
			editFormVisible: false,
			//编辑界面数据
			editForm: {
				id: 0,
				username: '',
				password: ''
			},
			editFormRules: {
				password: [
					{ required: true, message: '请输入密码', trigger: 'blur' },
				]
			}
		}
	},
	methods: {
		dateFormat,
		getUsers(page = 1) {
			let limit = this.limit;
			let start = (page - 1) * limit;
			userService.getUsers(page, limit).then((res) => {
				this.total = res.count;
				this.users = res.rows;
			});
		},
		handleAdd() {
			this.addFormVisible = true;
		},
		closeAdd: function() {
			this.addFormVisible = false;
			this.$refs.addForm.resetFields(); //字段重置 移除校验结果
		},
		addSubmit() {
			this.$refs.addForm.validate((valid) => {
				if (valid) {
					let params = this.addForm;
					userService.addUser(params).then(() => {
						// 更新列表
						this.getUsers();
						this.$message({
							message: '提交成功',
							type: 'success'
						});
						this.closeAdd();
					});
				}
			});
		},
		//删除
		handleDel: function(index, row) {
			this.$confirm('确认删除该记录吗?', '提示', {
				type: 'warning'
			}).then(() => {
				userService.deleteUser(row.id).then(() => {
					// 更新列表
					this.getUsers();
					this.$message({
						message: '删除成功',
						type: 'success'
					});
				});
			})
		},
		//显示编辑界面
		handleEdit: function(index, row) {
			this.editFormVisible = true;
			this.editForm = Object.assign({}, row);
		},
		closeEdit: function() {
			this.editFormVisible = false;
			this.$refs.editForm.resetFields(); //字段重置 移除校验结果
		},
		editSubmit: function() {
			this.$refs.editForm.validate((valid) => {
				if (valid) {
					let params = this.editForm;
					userService.updataUser(params).then(() => {
						// 更新列表
						this.getUsers();
						this.$message({
							message: '更新成功',
							type: 'success'
						});
						this.closeEdit();
					});
				}
			});
		},
	},
	mounted() {
		this.getUsers();
	}
}

</script>
